import {reqGetGoodInfo} from "@/api";
import {getUUID} from "@/utils/uuid_token";

const state = {
    // 商品信息
    goodInfo: {},
    // 游客身份的临时id
    uuid_token: getUUID()
}
const actions = {
    async getGoodInfo(context, skuId) {
        const result = await reqGetGoodInfo(skuId);
        if (result.code === 200) {
            context.commit('GETGOODINFO', result.data)
        }
    },
    /*async addOrUpdateShopCart({commit},{skuId, skuNum}) {
        const result = await reqAddOrUpdateShopCart(skuId, skuNum);
        if (result.code === 200) {
            return 'ok'
        } else {
            return Promise.reject(new Error('addShopCartFail'))
        }
    }*/
}
const mutations = {
    GETGOODINFO(state, data) {
        state.goodInfo = data
    }
}
const getters = {
    // 目录显示
    categoryView(state) {
        // 数据读取耗时，让他至少是个对象，不然会报undefined警告
        return state.goodInfo.categoryView || {}
    },
    // 商品信息
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    // 商品属性
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || []
    },
}
export default {
    state,
    actions,
    mutations,
    getters
}